package com.tsc.springTemp.springBeanLifeModel;
import java.lang.annotation.*;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: chentl
 * @Date: 2025/09/12/12:14
 * @Description:
 */
// 自定义注解


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface PostConstruct {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface PreDestroy {
}

// Bean 类
public class UserService implements BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, InitializingBean, DisposableBean {

    private String beanName;
    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;
    private String username;

    public UserService() {
        System.out.println("1. 构造函数调用 - Bean 实例化");
    }

    public void setUsername(String username) {
        this.username = username;
        System.out.println("2. 属性赋值 - setUsername: " + username);
    }

    @Override
    public void setBeanName(String name) {
        this.beanName = name;
        System.out.println("3. BeanNameAware.setBeanName: " + name);
    }


    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
        System.out.println("4. BeanFactoryAware.setBeanFactory");
    }


    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        System.out.println("5. ApplicationContextAware.setApplicationContext");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("7. @PostConstruct 方法调用");
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("8. InitializingBean.afterPropertiesSet");
    }

    public void customInit() {
        System.out.println("9. 自定义 init-method 调用");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("12. @PreDestroy 方法调用");
    }

    @Override
    public void destroy() {
        System.out.println("13. DisposableBean.destroy");
    }

    public void customDestroy() {
        System.out.println("14. 自定义 destroy-method 调用");
    }

    public void sayHello() {
        System.out.println("11. Bean 使用中: Hello, " + username + "!");
    }

    @Override
    public void setApplicationContext(org.apache.catalina.core.ApplicationContext applicationContext) {

    }

    @Override
    public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) {

    }
}
